import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

App.mpType = 'app'
// main.js
import uView from "uview-ui";

Vue.use(uView);
// 引入uView对小程序分享的mixin封装
let mpShare = require('uview-ui/libs/mixin/mpShare.js');
Vue.mixin(mpShare)

Vue.prototype.systemId = "92d4e728cbe430f005e4b442e199f8ad";

//同步系统系统信息
Vue.prototype.syncSystemInfo = function() {
	//系统信息
	const systemShare = uni.getStorageSync("systemShare");
	//今天是否已获取系统信息
	const systemShareToday = uni.getStorageSync("systemShareToday");
	if (this.isNull(systemShare) || this.getDay() != systemShareToday) {
		this.getSystemInfo();
	}
}

//获取系统信息
Vue.prototype.getSystemInfo = function() {
	this.$u.get(`/admin/systemBaseInfo/${this.systemId}`).then(res => {
		if (res.code != 200) {
			return;
		}
		uni.setStorageSync("systemShareToday", this.getDay());
		uni.setStorageSync("systemShare", res.data);
	});
}
//绑定父用户
Vue.prototype.boundParentUser = function(parentId) {
	const authorization = uni.getStorageSync('Authorization');
	const user = uni.getStorageSync("user");
	let inviteUserId = !this.isNull(parentId) ? parentId : uni.getStorageSync("inviteUserId");
	if (this.isNull(authorization) || this.isNull(inviteUserId)) {
		return
	}
	if (!this.isNull(user)) {
		if (!this.isNull(user.parentId) || user.parentId == inviteUserId || user.id == inviteUserId) {
			return;
		}
	}
	this.$u.get('/user/boundParent?parentId=' + inviteUserId).then(res => {});
}
//绑定邀请人
Vue.prototype.boundInviteUser = function(inviteId) {
	this.$u.get(`/user/boundParent?parentId=${inviteId}`).then(res => {});
}
//获取日期
Vue.prototype.getDay = function(index) {
	//昨天的时间
	var day1 = new Date();
	day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
	var s1 = day1.getFullYear() + "-" + (this.getNum(day1.getMonth() + 1)) + "-" + this.getNum(day1.getDate());
	//今天的时间
	var day2 = new Date();
	day2.setTime(day2.getTime());
	var s2 = day2.getFullYear() + "-" + (this.getNum(day2.getMonth() + 1)) + "-" + this.getNum(day2.getDate());
	//明天的时间
	var day3 = new Date();
	day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
	var s3 = day3.getFullYear() + "-" + (this.getNum(day3.getMonth() + 1)) + "-" + this.getNum(day3.getDate());
	if (index == -1) {
		return s1;
	} else if (index == 1) {
		return s3;
	} else {
		return s2;
	}
}
//获取数字
Vue.prototype.getNum = function(num) {
	if (num < 10) {
		return "0" + num
	}
	return num;
}
//获取用户信息
Vue.prototype.getLoginUserInfo = function() {
	this.$u.get('/user/getUserInfo').then(res => {
		if (res.code != 200) {
			return;
		}
		uni.setStorageSync("user", res.data);
	});
}
//获取分享信息
Vue.prototype.getShareInfo = function(url) {
	let path = this.isNull(url) ? "/pages/index/index" : url;
	const user = uni.getStorageSync("user");
	if (!this.isNull(user) && !this.isNull(user.id)) {
		if (path.indexOf("?") != -1) {
			path += ("&userId=" + user.id)
		} else {
			path += ("?userId=" + user.id)
		}
	}
	let shareInfo = {
		path: path
	}
	const systemShare = uni.getStorageSync("systemShare");
	if (!this.isNull(systemShare)) {
		shareInfo.title = systemShare.shareTitle;
		shareInfo.imageUrl = systemShare.shareCover;
	}
	console.log(shareInfo)
	return this.$u.mpShare = shareInfo;
}
//随机数
Vue.prototype.randomNum = function(start, end) {
	return Math.floor(Math.random() * (end - start) + start);
}
//判断师傅为空
Vue.prototype.isNull = function(str) {
	return str == null || str == '' || str == undefined;
}

Vue.mixin({
	methods: {
		setData: function(obj, callback) {
			let that = this;
			const handleData = (tepData, tepKey, afterKey) => {
				tepKey = tepKey.split('.');
				tepKey.forEach(item => {
					if (tepData[item] === null || tepData[item] === undefined) {
						let reg = /^[0-9]+$/;
						tepData[item] = reg.test(afterKey) ? [] : {};
						tepData = tepData[item];
					} else {
						tepData = tepData[item];
					}
				});
				return tepData;
			};
			const isFn = function(value) {
				return typeof value == 'function' || false;
			};
			Object.keys(obj).forEach(function(key) {
				let val = obj[key];
				key = key.replace(/\]/g, '').replace(/\[/g, '.');
				let front, after;
				let index_after = key.lastIndexOf('.');
				if (index_after != -1) {
					after = key.slice(index_after + 1);
					front = handleData(that, key.slice(0, index_after), after);
				} else {
					after = key;
					front = that;
				}
				if (front.$data && front.$data[after] === undefined) {
					Object.defineProperty(front, after, {
						get() {
							return front.$data[after];
						},
						set(newValue) {
							front.$data[after] = newValue;
							that.$forceUpdate();
						},
						enumerable: true,
						configurable: true
					});
					front[after] = val;
				} else {
					that.$set(front, after, val);
				}
			});
			// this.$forceUpdate();
			isFn(callback) && this.$nextTick(callback);
		}
	}
});
const app = new Vue({
	...App
})
// http拦截器，此为需要加入的内容，如果不是写在common目录，请自行修改引入路径
import httpInterceptor from '@/common/http.interceptor.js'
// 这里需要写在最后，是为了等Vue创建对象完成，引入"app"对象(也即页面的"this"实例)
Vue.use(httpInterceptor, app)
app.$mount()
